import apiClient from '@/lib/interceptor';

// 定义文章类型接口
export interface Article {
  id: number;
  title: string;
  content: string;
  publishDate: string;
  author: string;
  editor: string;
  viewCount: number;
  articleType: number;
  hasAttachment: boolean;
  status: number;
  attachments?: Attachment[];
}

// 定义附件类型接口
export interface Attachment {
  id: number;
  fileName: string;
  fileUrl: string;
  fileSize: number;
  fileType: string;
  downloadCount: number;
  uploadTime: string;
}

// API 响应接口
interface ApiResponse<T> {
  code: number;
  msg: string;
  data: T;
}

// 获取所有文章
export const getAllArticles = () => {
  return apiClient.get<ApiResponse<Article[]>>('/articles');
};

// 管理员获取所有文章(需要带token)
export const getAllArticlesWithToken = () => {
  return apiClient.get<ApiResponse<Article[]>>('/articles/admin')
}

// 根据ID获取文章详情
export const getArticleById = (id: number) => {
  return apiClient.get<ApiResponse<Article>>(`/articles/${id}`);
};

// 根据类型获取文章列表
export const getArticlesByType = (type: number) => {
  return apiClient.get<ApiResponse<Article[]>>(`/articles/type/${type}`);
};

// 创建文章
export const createArticle = (article: Partial<Article>) => {
  return apiClient.post<ApiResponse<Article>>('/articles/create', article);
};

// 更新文章
export const updateArticle = (id: number, article: Partial<Article>) => {
  return apiClient.put<ApiResponse<Article>>(`/articles/${id}/update`, article);
};

// 删除文章（软删除）
export const deleteArticle = (id: number) => {
  return apiClient.delete<ApiResponse<void>>(`/articles/${id}/delete`);
};

// 恢复文章 (撤销软删除)
export const restoreArticle = (id: number) => {
  return apiClient.put<ApiResponse<void>>(`/articles/${id}/restore`);
};

// 硬删除文章
export const hardDeleteArticle = (id: number) => {
  return apiClient.delete<ApiResponse<void>>(`/articles/${id}/hard-delete`);
};

// 文章类型枚举
export enum ArticleType {
  DEPARTMENT_INTRO = 1,    // 部门介绍
  ETHICS = 2,             // 师德师风
  DEVELOPMENT = 3,        // 教师发展
  ETHICS_REPORT = 4,      // 师德师风举报
  TITLE_REVIEW = 6,       // 职称评审
  DOWNLOADS = 7,          // 资料下载
  PROCESS = 8            // 办事流程
}

// 使用示例：
/*
import { getArticlesByType, ArticleType } from '@/lib/articles';

// 获取师德师风文章列表
const fetchEthicsArticles = async () => {
  try {
    const { data } = await getArticlesByType(ArticleType.ETHICS);
    if (data.code === 1) {
      setArticles(data.data);
    }
  } catch (error) {
    console.error('获取师德师风文章失败:', error);
  }
};
*/ 